﻿Imports Microsoft.VisualBasic
Imports System.Collections.Generic

Public Class MyOrganizationDataFilter
    Inherits MyStringBuilder

    Private url As String
    Private userID As String
    Private tableName As String

    Private listAll As Boolean = False

    Public Sub New()

    End Sub

    Public Sub New(ByVal listAll As Boolean)
        Me.listAll = listAll
    End Sub

    Public Sub New(ByVal tableName As String)
        Me.tableName = tableName
    End Sub

    Private WaitAppendList As New List(Of String)

    Public Overrides Function AppendFormat(ByVal format As String, ByVal ParamArray args() As Object) As System.Text.StringBuilder
        'Order by 子句要最後再加入
        If format.ToLower().Contains("order by") = True Then
            Dim s As New StringBuilder

            s.AppendFormat(format, args)

            Me.WaitAppendList.Add(s.ToString())
        Else
            Return MyBase.AppendFormat(format, args)
        End If


    End Function

    Public Overrides Function ToString() As String
        Dim t As New StringBuilder

        t.AppendFormat("select a.* ")
        t.AppendFormat(", v.AddAccess, v.EditAccess, v.DeleteAccess, v.QueryAccess, v.PrintAccess, v.AdminAccess, v.HaveAccess ")
        t.AppendFormat("from ({0}) a ", MyBase.s)

        If Me.tableName = "" Then
            t.AppendFormat("{2} GetOrganizationByAuth ('{0}','{1}') v on a.UOrganizationID = v.UOrganizationID ", clsAuth.GetUserID(), clsAuth.GetProgramName(), Me.JoinString)
        Else
            t.AppendFormat("{2} GetOrganizationByAuth ('{0}','{1}') v on a.UOrganizationID = v.UOrganizationID ", clsAuth.GetUserID(), tableName, Me.JoinString)
        End If

        For Each subSql As String In Me.WaitAppendList
            t.AppendFormat(subSql)
        Next

        'Me.s = t

        'Return MyBase.ToString()
        Return t.ToString()
    End Function

    Private ReadOnly Property JoinString()
        Get
            If Me.listAll = True Then
                Return "left join"
            Else
                Return "inner join"
            End If
        End Get
    End Property

End Class
